refactor(config): add validateOptionalInt to eliminate repeated nil-guard boilerplate#10059
Conversation
…rd duplication Adds a generic `validateOptionalInt(ptr *int, logMsg string, validateFn func(int) *ValidationError) error` helper to `internal/config/validation_rules.go` that consolidates the repeated nil-check-before-dereference pattern for optional `*int` fields in config validation. - Correctly handles the Go nil-interface pitfall: the `*ValidationError` return from `validateFn` is checked for nil before being returned as `error`. - Refactors `validateGatewayConfig` (4 occurrences) and `validateStandardServerConfig` (1 occurrence) to use the helper. - The zero-sentinel case in per-server `toolTimeout` (0 means inherit from gateway) is handled inline in the closure, preserving existing semantics. - Adds `TestValidateOptionalInt` covering nil pointer, valid value, invalid value, and the zero-sentinel guard pattern. Closes #10044
There was a problem hiding this comment.
Pull request overview
Refactors optional integer validation to centralize nil guarding while preserving existing validation semantics.
Changes:
- Adds and tests
validateOptionalInt. - Refactors gateway and per-server integer validation to use the helper.
- Preserves per-server zero-as-inherit behavior.
Show a summary per file
| File | Description |
|---|---|
internal/config/validation_rules.go |
Adds the shared validation helper. |
internal/config/validation_rules_test.go |
Tests nil, valid, invalid, and sentinel cases. |
internal/config/validation_gateway.go |
Refactors four optional gateway fields. |
internal/config/validation_server.go |
Refactors per-server tool timeout validation. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Medium
🔒 mcpg Read-Only Stress — gVisorSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Gateway enforcement note: Write MCP tools are entirely absent from the gateway tool manifest ( Overall: PASS References: §30161934639
|
🔒 mcpg Read-Only Stress — docker-sbxSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Gateway enforcement note: tools.json confirms gateway registers only read-only tools for the github MCP server. Write tools (star_repository, add_issue_reaction, create_branch, create_or_update_file, create_pull_request) are absent — gateway-level policy enforcement, not merely backend configuration. Overall: PASS References: §30161934629
|
🔒 mcpg Read-Only Stress — defaultSurface coverage: MCP tool calls + proxied CLI (REST) + GraphQL mutations
Gateway enforcement detail (Part B): All 7 write tools absent from the 22-tool read-only registry. Gateway returns MCP JSON-RPC Overall: PASS References: §30161934628
|
The config validation code repeated the same nil-check-before-dereference pattern ~5 times across
validation_gateway.goandvalidation_server.go, with no shared abstraction — increasing bug risk (forgetting the nil guard on new fields) and making the validation functions harder to scan.Changes
validation_rules.go— AddsvalidateOptionalInt(ptr *int, logMsg string, validateFn func(int) *ValidationError) error. Explicitly checks*ValidationError != nilbefore returning aserrorto avoid the typed-nil-interface pitfall.validation_gateway.go— Replaces 4 nil-guarded blocks (Port,StartupTimeout,ToolTimeout,PayloadSizeThreshold) withvalidateOptionalIntcalls.validation_server.go— Replaces the per-serverToolTimeoutnil-guard. The zero-means-inherit sentinel is handled inside the closure, preserving existing semantics.validation_rules_test.go— AddsTestValidateOptionalIntcovering: nil ptr (no-op), valid value, invalid value, closure with zero-sentinel guard.Example
Adding a new optional
*intfield now requires a singlevalidateOptionalIntcall rather than a copy-pasted nil-guard block.